import {Component,OnInit,ChangeDetectorRef} from '@angular/core';
import { ActionSheetController } from '@ionic/angular';
import { HttpService } from '../../.././services/http.service';
import { UpphotoService } from '../../../services/upphoto.service';
import { commService } from '../../../services/comm.service';
import { LoginInfoService } from '../../../services/login-info.service';
import { Router } from '@angular/router'
import { BaseUiService } from '../../../services/base-ui.service';

@Component({
  selector: 'app-suggest',
  templateUrl: './suggest.page.html',
  styleUrls: ['./suggest.page.scss'],
})
export class SuggestPage implements OnInit {
  public suggestlist:any
  public suggestId:any
  public suggestInfo:any
  public imgList:any = []
  public userInfo:any
  public suggestImglist:any
  public isShowimg:any
  constructor(
    public baseUiService:BaseUiService,
    public router: Router,
    public LoginInfoService:LoginInfoService,
    public commService: commService,
    public upphotoService:UpphotoService,
    public HttpService: HttpService,
    public actionSheetController: ActionSheetController,
    private cd: ChangeDetectorRef
  ) { }

  ngOnInit() {
    this.userInfo = this.LoginInfoService.getInfo()
    console.log(this.userInfo)
    this.seach()
  }
  seach(){
    this.HttpService.getsuggest({}).then(res =>{
      if(res['code']==200){
        this.suggestlist = res['items']
      }else{
        console.log(res['msg'])
      }
    })
  }
  //上传意见
  getsuggest(){
    let arr =[]
    console.log(this.imgList)
    this.imgList.forEach(e => {
      arr.push(e.unitGuId)
    });
    let p = {
      idCard:this.userInfo.idCard,
      name:this.userInfo.userName,
      phone:this.userInfo.mobile,
      content:this.suggestInfo,
      result:"",
      orderId:"",
      notaryId:"",
      annexId:arr.join(","),
      complaintTypeId:this.suggestId
    }
    this.HttpService.addsuggest(p).then(res =>{
      console.log(res,'++958')
      if(res['code']==200){
        this.baseUiService.showToast('提交成功！','success','top')
        this.router.navigate(['/tabs/tab4'])
      }else{
        this.baseUiService.showToast(res['msg'],'success','top')
      }
    }).catch(err =>{
      this.baseUiService.showToast(err,'customred','top')
    })
  }
   //底部弹出
   async presentActionSheet() {
    const actionSheet = await this.actionSheetController.create({
      header: '上传材料',
      buttons: [ {
        text: '拍照上传',
        icon: 'camera',
        handler: () => {
          this.commService.photo(400,40).subscribe(res=>{
            this.demo(res)
          })
        }
      }, {
        text: '相册上传',
        icon: 'image-outline',
        handler: () => {
          this.commService.picture(300,40).subscribe(res =>{
            this.demo(res)
          })
        }
      }, {
        text: '关闭',
        icon: 'close',
        role: 'cancel',
        handler: () => {
          console.log('关闭');
        }
      }]
    });
    await actionSheet.present();
  }


  demo(info:any){
    let _this =this
    this.upphotoService.Uploading(info).then( res1 =>{
      _this.imgList = [..._this.imgList,...res1]
      setTimeout(() => {
        this.cd.detectChanges();
      },200)
    })
  }

  imgClick(urlinfo:any){
    this.isShowimg = true
    this.suggestImglist = ''
    this.suggestImglist = urlinfo
  }
  iconclick(){
    this.isShowimg = false 
  }
}
